//
//  ModifyView.m
//  Daybook
//
//  Created by liutaifeng on 16/5/26.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "ModifyView.h"

@interface ModifyView ()
<
 UITableViewDataSource,
 UITableViewDelegate,
 UICollectionViewDataSource,
 UICollectionViewDelegate
>
@property (nonatomic, strong) UIView *topView;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *saveButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AccountBookModel *accountModel;
@property (nonatomic, strong) NSIndexPath *firstIndex;
@property (nonatomic, strong) NSIndexPath *secondIndex;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *picArray;
@property (nonatomic, assign) NSInteger newIndex;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) UIView *headLine;
@end

@implementation ModifyView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}
- (void)setModel:(AccountBookModel *)model {
    if (_model != model) {
        _model = model;
        self.name = _model.accountBookName;
        for (NSInteger i = 0; i < _picArray.count; i++) {
            if ([_model.accountBookCover isEqualToString:_picArray[i]]) {
                self.index = i;
                [_collectionView reloadData];
                break;
            }
        }
        
    }
}

- (void)createView {
    
    self.backgroundColor = [UIColor whiteColor];
    
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, MAIN_WIDTH, 44)];
    [self addSubview:_topView];
    _topView.backgroundColor = [UIColor whiteColor];
    
    self.headLine = [[UIView alloc] initWithFrame:CGRectMake(0, 43, MAIN_WIDTH, 1)];
    [_topView addSubview:_headLine];
    _headLine.backgroundColor = LINE_COLOR;
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(10, 7, 25, 25);
    [_topView addSubview:_backButton];
//    _backButton.backgroundColor = [UIColor redColor];
//    [_backButton setTitle:@"<" forState:UIControlStateNormal];
    [_backButton setImage:[UIImage imageNamed:@"cha.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _saveButton.frame = CGRectMake(MAIN_WIDTH - 45, 5, 30, 30);
//    _saveButton.backgroundColor = [UIColor redColor];
    [_topView addSubview:_saveButton];
    [_saveButton setImage:[UIImage imageNamed:@"duihao.png"] forState:UIControlStateNormal];
//    [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [_saveButton addTarget:self action:@selector(saveButton:) forControlEvents:UIControlEventTouchUpInside];
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, MAIN_WIDTH, MAIN_HEIGHT / 5) style:UITableViewStylePlain];
    [self addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = MAIN_HEIGHT / 10;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[AccountBookNameCell class] forCellReuseIdentifier:NSStringFromClass([AccountBookNameCell class])];
    [_tableView registerClass:[ModifyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ModifyTableViewCell class])];
    _tableView.scrollEnabled = NO;

    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(MAIN_WIDTH / 3, MAIN_HEIGHT / 4 - 30);
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, MAIN_HEIGHT / 5 + 100, MAIN_WIDTH, MAIN_HEIGHT - MAIN_HEIGHT / 5 - 100) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    [self.collectionView registerClass:[ModifyCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([ModifyCollectionViewCell class])];
    
    
    
    self.picArray = @[@"AccountBookCover1.png", @"AccountBookCover2.png", @"AccountBookCover3.png", @"AccountBookCover4.png", @"AccountBookCover5.png", @"AccountBookCover6.png"];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.picArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ModifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([ModifyCollectionViewCell class]) forIndexPath:indexPath];
    cell.picImageView.image = [UIImage imageNamed:_picArray[indexPath.row]];
    if (indexPath.row == self.index) {
        cell.backView.backgroundColor = [UIColor lightGrayColor];
    }
    else {
        cell.backView.backgroundColor = [UIColor whiteColor];
    }
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.index = indexPath.row;
    self.model.accountBookCover = _picArray[indexPath.row];
    [_tableView reloadRowsAtIndexPaths:@[_secondIndex] withRowAnimation:UITableViewRowAnimationNone];
    [_collectionView reloadData];
}




    

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        AccountBookNameCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AccountBookNameCell class]) forIndexPath:indexPath];
        cell.nameTextField.text = self.model.accountBookName;
        self.firstIndex = indexPath;
        return cell;
        
    }else {
        ModifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ModifyTableViewCell class]) forIndexPath:indexPath];
        [cell.picImageView setImage:[UIImage imageNamed:self.model.accountBookCover]];
        self.secondIndex = indexPath;
        return cell;
    }
}

- (void)backButton:(UIButton *)button {
    [self.delegate back];
}

- (void)saveButton:(UIButton *)button {
    AccountBookNameCell *accountCell = [_tableView cellForRowAtIndexPath:_firstIndex];
//    ModifyTableViewCell *modifyCell = [_tableView cellForRowAtIndexPath:_secondIndex];
    self.model.accountBookName = accountCell.nameTextField.text;
    
    BOOL result = [self select];
    if (result == YES) {
        [self.delegate saveModel:self.model];
    }
    
}
- (BOOL)select {
    
    NSMutableArray *array = [[AccountBookSQL shareDataBaseTool] select];
    if (![self.model.accountBookName isEqualToString:self.name]) {
        for (AccountBookModel *model in array) {
            if ([self.model.accountBookName isEqualToString:model.accountBookName]) {
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"账本名重复, 请重新输入" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [alert show];
                return NO;
            }
            
            
        }
    }
    
        
    
    return YES;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
